<template>
  <xdh-tree-select
    :placeholder="placeholder"
    :data="treeData"
    :props="props"
    :check-strictly="false"
    :default-expand-all="false"
    :node-key="nodeKey"
    :show-checkbox="multiSelect"
    :highlight-current="!multiSelect"
    :show-select-num="showSelectNum"
    :width="250"
    :value="Array.isArray(value)?value:[]"
    @on-selected="onSelected"></xdh-tree-select>
</template>

<script>
  /**
   * 组织下拉树选择
   * 参数
   *  value Array 传入数据 默认 []
   *  show-select-num Boolean  输入框是否显示选择项数 true
   *  placeholder 输入框placeholder
   *  @on-selected   响应勾选对象数组集合
   *  @on-selected-keys  响应勾选对象pkValue 数据集合
   *  示例
   *    <select-tree-org :value="['']" :show-select-num="false"></select-tree-org>
   */
  import XdhTreeSelect from '../widgets/xdh-tree-select'
  import {getOrgTree} from '../helper/before'
  import {treeFetchParent} from '../helper/tree'

  export default {
    name: 'select-tree-org',
    components: {
      XdhTreeSelect
    },
    props: {
      value: {
        type: Array,
        default() {
          return []
        }
      },
      // 输入框显示选择的项
      showSelectNum: {
        type: Boolean,
        default: true
      },
      // placeholder
      placeholder: {
        type: String,
        default: '选择组织'
      },
      // 是否多选 默认单选
      multiSelect: {
        type: Boolean,
        default: false
      },
      // 是否可以添加数据 默认不能
      isAddData: {
        type: Boolean,
        default: false
      },
      sameOrg: {
        type: Object,
        default: null
      }
    },
    directives: {},
    mixin: [],
    data() {
      return {
        props: {
          children: 'children',
          label: 'label'
        },
        nodeKey: 'id'
      }
    },
    computed: {
      treeData() {
        let orgDataTree = this.$store.state.sysOrgTree.orgTree
        let filterOrgRoot = (d) => {
          return d.map(res => {
            return res.pid !== '' ? res[this.props.children] : res
          })[0]
        }
        if (!orgDataTree) {
          getOrgTree().then(res => {
            if (!Array.isArray(res)) {
              orgDataTree = [res]
            }
            let obj = [filterOrgRoot(orgDataTree)];
            this.isAddData && obj && obj.push(this.sameOrg)
            return obj;
          })
        } else {
          if (!Array.isArray(orgDataTree)) {
            orgDataTree = [orgDataTree]
          }
          // console.log(filterOrgRoot(orgDataTree))
          let obj = [filterOrgRoot(orgDataTree)];
          this.isAddData && obj && obj.push(this.sameOrg)
          return obj;
        }
      }
    },
    watch: {},
    methods: {
      onSelected(nodes) {
        let selectNode = nodes
        // 结构树勾选过滤保留父级
        selectNode = treeFetchParent(nodes, this.nodeKey, this.props)
        // 响应勾选对象数组集合
        this.$emit('on-selected', selectNode);
        // 响应勾选对象pkValue 数据集合
        let selectNodeKeys = selectNode.map(n => {
          return n[this.nodeKey]
        });
        this.$emit('on-selected-keys', selectNodeKeys)
      }
    },
    created() {
    }
  }
</script>

<style scoped>

</style>
